var GameHall = (function(){
		var playerList ={};
		var roomList ={};
		/* class / object definition begin*/
		/* display the info of logged user */
		var MyInfoPanel ={
			addMe:function(me){
				var info = new PlayerInfo(me);
				$('myInfoPanel').appendChild(info.getDom());
				playerList[me.id] = info; 
			}
		};
		
		var RoomListPanel ={
			cols:3,
			rows:4,
			init:function(){
				this.dom = $('roomListPanel');
				var n=1;
				for(var i=0;i<this.rows;i++){
					for(var j=0;j<this.cols;j++){
						var room = new Room(n++);
						roomList[room.roomid] = room;
						var ele = room.getDom();
						ele.style.left=j*200;
						ele.style.top =i*150+50;
						this.dom.appendChild(ele);
					}
				}
			}
		};
		
		var UserListPanel ={
			init:function(){
				this.dom = $('userListPanel');
				this.list1 = $('listp1');
				this.list2 = $('listp2');
			},
			addPlayer:function(player,event){
				var info = new PlayerInfo(player);
				this.list1.appendChild(info.getDom());
				playerList[player.id] = info;
				this.displayInfo("["+player.name+"] entered the hall.");
			},
			removePlayer:function(player){
				this.list1.removeChild(playerList[player.id].getDom());
				this.displayInfo("["+player.name+"] left the hall.");
				delete playerList[player.id];
				
			},
			displayInfo:function(info){
				var p = document.createElement("div");
				p.innerText = info;
				this.list2.appendChild(p);
				p.scrollIntoView();
			}
		};
		
		/* class Player*/
		function Player(id,name){
			this.id = id;
			this.name = name;
		}
		/* class Room*/
		function Room(id){
			this.roomid=id;
			this.seatA_p = null;
			this.seatB_p= null;
			this.dom = $('tmp4room').cloneNode(true);
			this.dom.style.display="block";
			this.seatA_img = this.dom.getElementsByTagName('img')[0];
			this.seatB_img = this.dom.getElementsByTagName('img')[2];
			this.table_img = this.dom.getElementsByTagName('img')[1];
			this.seatA = this.dom.getElementsByTagName('div')[0];
			this.seatB = this.dom.getElementsByTagName('div')[1];
			this.init();
		}
		Room.prototype ={
			init:function(){
				var that = this;
				this.seatA_img.onclick = function(){
					if(that.seatA_p!=null){alert("people on seat.");return}
					var param ="rid="+that.roomid+"&pid="+pid+"&seat=A";
					new Ajax().send("EnterRoom",{"data":param,"onsuccess":function(){
						openRoom({"seat":"A","pid":pid,"pname":pname,"rid":that.roomid});
					}});
				};
				this.seatB_img.onclick = function(){
					if(that.seatB_p!=null){alert("people on seat.");return}
					var param ="rid="+that.roomid+"&pid="+pid+"&seat=B";
					new Ajax().send("EnterRoom",{"data":param,"onsuccess":function(){
						openRoom({"seat":"B","pid":pid,"pname":pname,"rid":that.roomid});
					}});
				};
			},
			getDom:function(){
				return this.dom;
			},
			active:function(){
				this.table_img.src="image/table1.png";
			},
			inactive:function(){
				this.table_img.src="image/table2.png";
			},
			setSeatA:function(pinfo){
				if(pinfo){
					this.seatA_p = pinfo.player;
					this.seatA_img.src="image/portrait.png";
					this.seatA.innerText = pinfo.player.name;
				}else{
					this.seatA_p = null;
					this.seatA_img.src="image/placeholder.png";
					this.seatA.innerText = "";
				}
			},
			setSeatB:function(pinfo){
				if(pinfo){
					this.seatB_p = pinfo.player;
					this.seatB_img.src="image/portrait.png";
					this.seatB.innerText = pinfo.player.name;
				}else{
					this.seatB_p = null;
					this.seatB_img.src="image/placeholder.png";
					this.seatB.innerText = "";
				}
			},
			setStatus:function(status){
				if(status=="active"){
					this.active();
				}else{
					this.inactive();
				}
			}
		}
		/* class PlayerInfo*/
		function PlayerInfo(player){
			this.player = player;
			this.dom = $('tmp4player').cloneNode(true);
			this.dom.style.display="block";
			this.idtext = this.dom.getElementsByTagName('span')[0];
			this.nametext = this.dom.getElementsByTagName('span')[1];
			this.init.call(this);
		}
		PlayerInfo.prototype = {
			init:function(){
				this.idtext.innerText = this.player.id;
				this.nametext.innerText = this.player.name;
			},
			getDom:function(){
				return this.dom;
			}
		};
		/* class / object definition end*/
		function startService(){
			var ajax = new Ajax();
			setInterval(function(){
				try{
				ajax.send("AjaxFetchMsg",{"data":"pid="+pid,"onsuccess":function(req){
					var xml = req.responseXML.documentElement;
					if(xml!=null){
						parseXML(xml.getElementsByTagName('message'));
					}
				}});
				}catch(e){alert(e.message);}
			},200);
		}
		function parseXML(msgs){
			for(var i =0;i<msgs.length;i++){
				var forwhich =msgs[i].getAttribute('for');
				if(forwhich==="user"){
						parseUserListXML(msgs[i])	
				}else{
						parseRoomXML(msgs[i]);
				}
			}
		}
		function parseUserListXML(msg){
			var e = msg.getAttribute("event");
			var pname = msg.getAttribute("pname");
			var pid = msg.getAttribute("pid");
			if(e==="enter"){
				UserListPanel.addPlayer(new Player(pid,pname));
			}else{
				UserListPanel.removePlayer(new Player(pid,pname));
			}
		}
		function parseRoomXML(msg){
			var roomid = msg.getAttribute("roomid");
			var room = roomList[roomid];
			var prop = msg.getAttribute("prop");
			var value =  msg.getAttribute("value");
			if(prop==="seatA"){
				room.setSeatA(playerList[value]);
			}else if(prop==="seatB"){
				room.setSeatB(playerList[value]);
			}else if(prop==="status"){
				room.setStatus(value);
			}
		}
		
		function openRoom(data){
			var url ="gameroom.html";
			var width = 800;
			var height =800;
			var left = (screen.availWidth-width)/2;
			var top =(screen.availHeight-height)/2;
			window.showModalDialog(url,data,"dialogWidth:"+width+"px;dialogHeight:"+height+
									   "px;dialogTop:"+top+"px;dialogLeft:"+left+
									   	"px;center:no;help:no;resizable:no;status:no;scroll:no"			
							);
		}
		function init(){
			window.onbeforeunload=function(){
    			new Ajax().send("LeaveHall",{"data":"pid="+pid});
    		};
			var me = new Player(pid,pname);
			MyInfoPanel.addMe(me);
			RoomListPanel.init();
			UserListPanel.init();
			startService();
		}
		
		return {init:init}
})()